#Suggestion or improvements

1 messages · Page 1 of 1 (latest)

cloud current
#

What do you guys think of this automation to check every night if there are lights or stuff open or on when they should be off or vice versa.

description: It will check everything is correctly working and if not it will let me know.
triggers:
  - trigger: time
    at: "22:00:00"
conditions: []
actions:
  - choose:
      - conditions:
          - condition: state
            entity_id: light.outside
            state: "off"
        sequence:
          - action: notify.mobile_app_filippos_phone
            data:
              message: Outside lights are Off!
              title: Warning!
      - conditions:
          - condition: state
            entity_id: light.cantina_light
            state: "on"
        sequence:
          - action: notify.mobile_app_filippos_phone
            data:
              message: Cantina light is On!
              title: Warning!
      - conditions:
          - condition: state
            entity_id: light.livingroom_lamp_light
            state: "on"
        sequence:
          - action: notify.mobile_app_filippos_phone
            data:
              message: Lamp (living room) light is On!
              title: Warning!
      - conditions:
          - condition: state
            entity_id: switch.studio_lamp_outlet
            state: "on"
          - condition: state
            entity_id: switch.studio_globe_outlet
            state: "on"
        sequence:
          - action: notify.mobile_app_filippos_phone
            data:
              message: Studio lights are On!
              title: Warning!
      - conditions:
          - condition: state
            entity_id: light.livingroom_led
            state: "on"
        sequence:
          - action: notify.mobile_app_filippos_phone
            data:
              message: Led (Tv) lights are On!
              title: Warning!
      - conditions:
          - condition: state
            entity_id: cover.outside_awning
            state: closed
        sequence:
          - action: notify.mobile_app_filippos_phone
            data:
              message: Awning outside is Open!
              title: Warning!
mode: single```
glad badgerBOT
#

To format your text as code, enter three backticks on the first line, press Enter for a new line, paste your code, press Enter again for another new line, and lastly three more backticks.
```yaml
example: here
```
Don't forget you can edit your post rather than repeatedly posting the same thing.

sly ginkgo
#

What about just creating scene with desired night mode which gets activated at 10pm? And not sending notification for each forgotten light?

cloud current
#

what would scene do in this case?

obtuse flint
#

a scene remembers the starts of all the devices you add and can strore them all togehter.
so instead of getting a "warning x light is on" you have a "nighttime" scene which switches on/off lights to match your predesigned scene

cloud current
#

thats why i used notifications

obtuse flint
#

fair enough, instead of making loads of different notifications I template 1 big notification that collates the data and displays everything you need on one. would hate to potentially get spammed

tall shard
#

I would use a "group" and implement a loop on all items.
I would move all logic in a script. ( I prefer no complex logic in automations for reusability).. a script with something like:

fields:
  p_entity_grp:
    name: p_entity_grp
    description: "name of group of entities "
    example: group.grp_entities_bat
    default: group.grp_entities_bat
    selector:
      entity: null

and a variable to perform the logic and "collect" all messages

variables:
  new_status: |-
    {% set ns = namespace() %}
    ....
    {%- for entity_id in expand (p_entity_grp) -%}
    
>>>>> insert logic to collect messages for each "entity_id" here

    {% endfor %}

that way if tomorrow you want to check another group you just need to provide the right group to the script, everything is done and reusable

cloud current
cloud current
obtuse flint
safe plinth
#

here's something that you can play with. This is a script; if your are using the UI just create a new script, switch to YAML, and paste this in.

#
alias: Notify if Entity States Do Not Match Desired States
description: ""
sequence:
  - variables:
      entity_dict_desired:
        light.outside: "on"
        light.cantina_light: "off"
        light.livingroom_lamp_light: "off"
        switch.studio_lamp_outlet: "off"
        switch.studio_globe_outlet: "off"
        light.livingroom_led: "off"
        cover.outside_awning: "closed"
      friendly_dict: >
        {% set ns = namespace(friendly_dict_output=[]) %}
        {% for entity, desired_state in entity_dict_desired.items() %}
          {% if states(entity) != desired_state %}
            {% set ns.friendly_dict_output = ns.friendly_dict_output + [(state_attr(entity, 'friendly_name') | trim, state_translated(entity))] %}
          {% endif %}
        {% endfor %}
        {{ ns.friendly_dict_output }}
      entity_count: >
        {{ friendly_dict | count }}
  - condition: template
    value_template: "{{ entity_count > 0 }}"
  - action: notify.mobile_app_filippos_phone
    metadata: {}
    data:
      title: "Warning!"
      message: "{{ friendly_dict | map('join',' is ') | join(' \n') ~ '.' }}"
safe plinth
#

forgot to ping @cloud current ^

sly ginkgo
#

Scripts work so that you have automation which triggers with specific condition and executes the script?

safe plinth
cloud current
safe plinth
# cloud current never used a script before, how would this work? how would the script improve m...

A script is essentially just the “actions” section of an automation. It’s easier to test (since you can just run it on demand) and it gives you the ability to reuse those actions in many different automations.

For your purposes, don’t get caught up in the difference. Just try it out. If you want to copy the logic over into the “actions” section of an automation because you’re more comfortable with that, then do it.

safe plinth
#

And in regards to what you need to set up to make it work, it’s just the steps I already laid about above the code. In the UI go to settings -> automations and scenes -> scripts -> create script -> create a new script -> three dots in upper right -> edit in YAML -> delete everything -> paste in the code I shared above -> save script -> run script

cloud current
#

dang ok ill let u know ty

#

damn that is sick

cloud current
#

for example the Tv leds lights will only send me the notification if the tv is off, because if it is on means that someone is using it and the leds needs to be on

safe plinth
#

To add entities you just add them to the dictionary at the beginning of the script

safe plinth
safe plinth
#

This version is simpler and would be easy for you to add and remove entities and conditions in the UI. After you create the script and verify it works, you can add new entities along with conditions by duplicating the entire if/then statement (which can also be done in the UI).

I made up the switch.livingroom_tv so you’ll have to replace that with a real entity.

#
alias: Notify if Entity States Do Not Match Desired States (Simple)
description: ""
sequence:
  - variables:
      result: []
  - if:
      - condition: state
        state: "on"
        entity_id: light.outside
    then:
      - variables:
          result: |
            {{ result + ["Outside light is on"] }}
  - if:
      - condition: state
        state: "on"
        entity_id: switch.livingroom_tv
      - condition: state
        state: "off"
        entity_id: light.livingroom_led
    then:
      - variables:
          result: "{{ result + ['TV is on but LEDs are off'] }}"
  - condition: template
    value_template: "{{ result | count > 0 }}"
  - action: mobile_app_filippos_phone
    metadata: {}
    data:
      title: "Warning!"
      message: "{{ result | join('\\n') }}"
cloud current
safe plinth
#

The original script I provided is made so that it is easy to add or remove entities but it is not set up for adding different conditions. If you’re not familiar with jinja and don’t plan to dive into it, you’ll need a different approach for an automation (or script) that is easier for you to maintain. I would suggest using the new script I provided. You should be able to see how simple it is to add entities and add conditions inside the UI. It is much more similar to the original automation you created yourself, with the difference that there is only a single notification that occurs at the end, instead of notifying you for each check.

cloud current
safe plinth
#

Everything, including code, can be edited in the UI. The only code you’d need to edit should be very obvious what it does and what needs changed.

Each “conditionally execute an action” block is where it tests whether an entity is in a certain state. This is what you did in your own code. Except you used choose instead of if/then which is wrong for this application

#

So, you can duplicate the “conditionally execute an action” block (using the “duplicate” option in the UI) and make sure the new block is next to all the other “conditionally execute” blocks. The add/remove conditions as needed, change the entity, etc. You will also see a variable where you can change the message that will be sent to your phone.

Spend a few minutes looking at it to understand it, and let me know if you have questions.