TRIGGERcmd
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Register
    • Login

    Run one of your TRIGGERcmd commands from a python script via Home Assistant

    Scheduled Pinned Locked Moved Home Assistant
    1 Posts 1 Posters 776 Views 1 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • RussR Offline
      Russ
      last edited by Russ

      Get your HA "Long-lived access tokens" from the Security page under your user profile. Click your HA username in the lower left, to open your user profile.

      You should see this at the top of the page:
      f9d78abf-2992-4714-86d1-df45a2813cf5-image.png

      import requests
      
      # Home Assistant settings
      BASE_URL = "http://(your HA server IP):8123/api"
      TOKEN = "(your long-lived HA access token)"
      
      # Headers for authentication
      HEADERS = {
          "Authorization": f"Bearer {TOKEN}",
          "Content-Type": "application/json"
      }
      
      def toggle_light(entity_id):
          """Toggle a light entity in Home Assistant."""
          url = f"{BASE_URL}/services/light/toggle"
          data = {"entity_id": entity_id}
          
          response = requests.post(url, json=data, headers=HEADERS)
          
          if response.status_code == 200:
              print(f"Successfully toggled {entity_id}.")
          else:
              print(f"Failed to toggle {entity_id}. Status code: {response.status_code}, Response: {response.text}")
      
      # Example usage
      if __name__ == "__main__":
          light_entity_id = "switch.laptop_calculator"  # Replace with your entity ID
          toggle_light(light_entity_id)
      

      The above script shows how to flip a switch, including the virtual switches you get from the official TRIGGERcmd integration.

      Russell VanderMey

      1 Reply Last reply Reply Quote 0

      Hello! It looks like you're interested in this conversation, but you don't have an account yet.

      Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

      With your input, this post could be even better 💗

      Register Login
      • First post
        Last post