• TedZanzibar@feddit.uk
    link
    fedilink
    English
    arrow-up
    2
    ·
    3 days ago

    Curious as to what you’re using scripts for? I have 88 automations and have so far found no need for a single script and I feel like I’m missing a trick somewhere.

    Yes, I do have some automations that share functionality but it’s one or two actions and it seems redundant to call a separate script.

    • Ludicrous0251@piefed.zip
      link
      fedilink
      English
      arrow-up
      3
      ·
      3 days ago

      Scripts are used for a lot of things! Generally any time I want to do the same thing under multiple automations, I use a script as the middleman.

      For example, I built a script for turning off all of my lights each night but it has to be fed a time scaling variable to determine specifically how quickly to turn them off (sometimes I want them turned off quickly vs slowly). Automations trigger said script with the right scale factor.

      I have air purifiers that I ramp up and down conditionally, they don’t have a built in ramping function so I built a script to ramp them to a target % in their allowed 10% increments, over a variable time period. This can be then be called in one line from any automation or script.

      When I’m turning off lights each night, I want them to ramp down to a specific level/color/temperature before turning off but only if they are currently on. Rather than build an if statement for every light, a script takes a input list of lights and runs through each one to determine whether or not to ramp.

      Finally, my Google Home device is able to call scripts directly, (“hey Google, activate Cozy Time” triggers the “Cozy Time” script) so some things I use Google Assistant to trigger use scripts directly since that was at least easier at the time than using an automation. If I automate the same thing (e.g., a “Cozy Time” button above), I can just call the script from the automation in one line, easy peasy.

      • TedZanzibar@feddit.uk
        link
        fedilink
        English
        arrow-up
        1
        ·
        2 days ago

        Thank you, that’s food for thought at least.

        Can I ask about your light script? I have a bunch of smart bulbs that either don’t support or don’t expose the ‘power-on behaviour’ option, so in a power cut they come on full bright when power is restored, often in the middle of the night.

        My HA is on a UPS so I’ve been trying to have it store the states of lights when the UPS switches to battery power (before they go to unavailable) and then restore those states when power comes back, but it’s apparently way beyond my skill set. Curious as to how your “input list of lights” works and whether it could help me…

        • Ludicrous0251@piefed.zip
          link
          fedilink
          English
          arrow-up
          2
          ·
          edit-2
          19 hours ago

          Here is the full script in case it’s helpful. took a hot second of searching to set everything up, but now it’s really easy to use. When you call the script inside an automation it has input fields just like if you’re calling a built-in function like light.turn_on

          For your specific use case though, it may be easier to just take advantage of the built-in Scenes function. You can use an “entity snapshot” with “Scene: Create” a scene of the current state of your “bad” lights when the power goes out, then “Activate” that scene, perhaps with a couple of seconds transition time to smooth things out as soon as power is restored.

          I use a similar scene based function to create flashing colored light alerts based on certain conditions.

          alias: Light conditional smart dimming (Kelvin)
          sequence:
            - repeat:
                for_each: "{{ lights }}"
                sequence:
                  - variables:
                      light_state: "{{states(repeat.item)}}"
                      timescale: "{{states('input_number.timescale')}}"
                  - if:
                      - condition: template
                        value_template: "{{light_state == 'on'}}"
                    then:
                      - metadata: {}
                        data:
                          brightness_pct: "{{target_brightness}}"
                          transition: "{{transition_rate * timescale}}"
                          kelvin: "{{color_temperature}}"
                        target:
                          entity_id: "{{repeat.item}}"
                        action: light.turn_on
          fields:
            lights:
              selector:
                entity:
                  multiple: true
                  filter:
                    - domain: light
              name: Light(s)
              required: true
            target_brightness:
              selector:
                number:
                  min: 1
                  max: 100
              name: Target brightness (%)
              default: 1
              required: true
            color_temperature:
              selector:
                color_temp:
                  unit: kelvin
                  min: 1500
                  max: 7000
              name: Color temperature
              required: true
              default: 2200
            transition_rate:
              selector:
                number:
                  min: 1
                  max: 600
              name: Transition rate
              description: Transition rate, scaled by 'input_number.timescale'
              required: true
              default: 100
          description: Dims the target light(s) if they are on - Kelvin setpoint.
          icon: mdi:lightbulb-auto-outline
          mode: parallel
          max: 15
          
          • TedZanzibar@feddit.uk
            link
            fedilink
            English
            arrow-up
            1
            ·
            21 hours ago

            Got a little help from CGPT so it might not be perfect, but this seems to work from my limited testing:

            triggers:
              - trigger: state
                entity_id:
                  - input_boolean.ups_power
            conditions: []
            actions:
              - choose:
                  - conditions:
                      - condition: state
                        entity_id: input_boolean.ups_power
                        state:
                          - "on"
                    sequence:
                      - action: scene.create
                        data:
                          scene_id: light_states_backup
                          snapshot_entities: |
                            {{ states.light | map(attribute='entity_id') | list }}
                  - conditions:
                      - condition: state
                        entity_id: input_boolean.ups_power
                        state:
                          - "off"
                    sequence:
                      - action: scene.turn_on
                        target:
                          entity_id: scene.light_states_backup
                        data: {}
                      - delay:
                          hours: 0
                          minutes: 0
                          seconds: 10
                          milliseconds: 0
                      - action: scene.delete
                        data:
                          entity_id: scene.light_states_backup
            mode: single
            

            I’ve only tested it by toggling the UPS boolean manually and not actually cutting the power, so I’m probably going to need to add a delay, or a retry loop or something to make sure the scene applies consistently but so far so good! Thanks for the inspiration.

            • Ludicrous0251@piefed.zip
              link
              fedilink
              English
              arrow-up
              1
              ·
              19 hours ago

              snapshot_entities: | {{ states.light | map(attribute='entity_id') | list }}

              This is the only part I’m unsure about, seems like a clunky way to get all of the lights, but if only certain ones are causing problems, I’d just put the problem ones here as a list (or create a light group in HA and only call that group

              Also, input_boolean.ups_power feels like ChatGPT is assuming you have a helper for the current power status, but I would just call directly from the UPS entity. You should be able to clean this up in the GUI.

              • TedZanzibar@feddit.uk
                link
                fedilink
                English
                arrow-up
                1
                ·
                18 hours ago

                That first line is what CGPT helped me with. I wanted something that I don’t need to modify when I add or remove lights, so this just gets everything. Ideally I’d just get the lights that don’t have the power restore feature but most of my lights go via Hue and that doesn’t expose the feature to HA at all.

                The input_boolean is a thing I already had setup. The UPS fires a webhook event when it goes in and out of battery mode and there’s a separate automation that switches the helper based on those.

          • TedZanzibar@feddit.uk
            link
            fedilink
            English
            arrow-up
            1
            ·
            1 day ago

            Thank you. I’ve also never used scenes beyond what comes built-in with Hue! This is all good stuff.