Help with automation templating

Hi, right now I have 4 different automations for 4 different cameras, one of them would be like this:

- id: 'Foto Movimiento Bajo'
  alias: Foto Movimiento Bajo
  initial_state: true
  trigger:
  - entity_id: binary_sensor.sensor_movimiento_acceso_bajo
    from: 'off'
    platform: state
    to: 'on'
  condition: []
  action:
  - data:
      entity_id: camera.bajo
      filename: '/config/www/snapshot/video_{{ entity_id.entity_id }}_{{ now().strftime("%d-%m-%Y %H.%M") }}.mp4'
      duration: 60
    service: camera.record
  - delay: 00:00:05
  - data:
      entity_id: image_processing.doods_bajo
    service: image_processing.scan
  - delay: 00:00:05
  - above: '0'
    condition: numeric_state
    entity_id: image_processing.doods_bajo
  - data:
      data:
        attachment: /config/www/snapshot/image_processing/bajo_latest.jpg
        html: "1"
      title: "\U0001F4F9 MOVIMIENTO DETECTADO"
      message: Se ha detectado movimiento en la entrada del bajo.
    service: notify.pushover   
  - delay: 00:01:30
  - service: shell_command.borrado_camara
  - service: shell_command.borrado_image_processing

The idea was to turn all of them into one, after looking at the topics I got something, but I have reached a point where I got stuck:

- id: 'prueba'
  alias: prueba
  initial_state: true
  trigger:
  - entity_id: binary_sensor.sensor_movimiento_acceso_bajo
    from: 'off'
    platform: state
    to: 'on'
  - entity_id: binary_sensor.sensor_movimiento_terraza
    from: 'off'
    platform: state
    to: 'on'
  - entity_id: binary_sensor.sensor_movimiento_acceso_garaje
    from: 'off'
    platform: state
    to: 'on'
  - entity_id: binary_sensor.sensor_movimiento_acceso_entrada    
    from: 'off'
    platform: state
    to: 'on' 
  action:
  - service: camera.record
    data_template: 
      entity_id: >
        {% set mapper = {
          'binary_sensor.sensor_movimiento_acceso_bajo':'camera.bajo',
          'binary_sensor.sensor_movimiento_terraza':'camera.terraza',
          'binary_sensor.sensor_movimiento_acceso_garaje':'camera.garaje',
          'binary_sensor.sensor_movimiento_acceso_entrada':'camera.entrada' } %}
        {{ mapper[trigger.entity_id] }}
      filename: >
        {% set mapper = {
          'binary_sensor.sensor_movimiento_acceso_bajo':'/config/www/snapshot/video_camara_bajo_{{ now().strftime("%d-%m-%Y %H.%M") }}.mp4',
          'binary_sensor.sensor_movimiento_terraza':'/config/www/snapshot/video_camara_terraza_{{ now().strftime("%d-%m-%Y %H.%M") }}.mp4',
          'binary_sensor.sensor_movimiento_acceso_garaje':'/config/www/snapshot/video_camara_garaje_{{ now().strftime("%d-%m-%Y %H.%M") }}.mp4',
          'binary_sensor.sensor_movimiento_acceso_entrada':'/config/www/snapshot/video_camara_entrada_{{ now().strftime("%d-%m-%Y %H.%M") }}.mp4' } %}
        {{ mapper[trigger.entity_id] }}
      duration: 60  
  - delay: 00:00:05
  - service: image_processing.scan
    data_template: 
      entity_id: >
        {% set mapper = {
          'binary_sensor.sensor_movimiento_acceso_bajo':'image_processing.doods_bajo',
          'binary_sensor.sensor_movimiento_terraza':'image_processing.doods_terraza',
          'binary_sensor.sensor_movimiento_acceso_garaje':'image_processing.doods_garaje',
          'binary_sensor.sensor_movimiento_acceso_entrada':'image_processing.doods_entrada' } %}
        {{ mapper[trigger.entity_id] }}
  - delay: 00:00:05   

I am not able to see the numerical value of the different entities in a template and continue with the automation,
I hope there is someone who can help me complete this.
Regards, and thank you very much

- id: 'prueba'
  alias: prueba
  initial_state: true
  trigger:
  - platform: state
    entity_id:
    - binary_sensor.sensor_movimiento_acceso_bajo
    - binary_sensor.sensor_movimiento_terraza
    - binary_sensor.sensor_movimiento_acceso_garaje
    - binary_sensor.sensor_movimiento_acceso_entrada
    to: 'on' 
  action:
  - service: camera.record
    data_template: 
      entity_id: >
        camera.{{ trigger.entity_id.rsplit('_',1)[1] }}
      filename: >
        /config/www/snapshot/video_camara_{{
          trigger.entity_id.rsplit('_',1)[1]
        }}_{{ now().strftime("%d-%m-%Y %H.%M") }}.mp4
      duration: 60  
  - delay: 00:00:05
  - service: image_processing.scan
    data_template: 
      entity_id: >
        image_processing.doods_{{ trigger.entity_id.rsplit('_',1)[1] }}
  - delay: 00:00:05
  - condition: template
    value_template: >
      {{ states(
           'image_processing.doods_' ~ trigger.entity_id.rsplit('_',1)[1]
         )|float > 0 }}
  - service: notify.pushover
    data_template:
      data:
        attachment: >
          /config/www/snapshot/image_processing/{{
            trigger.entity_id.rsplit('_',1)[1]
          }}_latest.jpg
        html: "1"
      title: "\U0001F4F9 MOVIMIENTO DETECTADO"
      message: >
        Se ha detectado movimiento en la entrada del {{
            trigger.entity_id.rsplit('_',1)[1]
        }}.
  - delay: 00:01:30
  - service: shell_command.borrado_camara
  - service: shell_command.borrado_image_processing
2 Likes

I knew you were one of those who could help me, thank you very much for the help …
Just one note. In the Notify service, you forgot a space :slight_smile:
where can I find information about the

{{ trigger.entity_id.rsplit('_',1)[1] }}

( 'image_processing.doods_' ~ trigger.entity_id.rsplit('_',1)[1] )

I would like to be able to understand things and learn a little more
again … thank you very much

Out of curiosity, where?

Well the first one is just Python. Since entity_id is a Python str object you can use normal String methods.

For the second one the only “unusual” part is the tilde operator (~). That is explained in the Jinja2 documentation under Other Operators.

1 Like

That’s a mighty powerful and handy little tilde. :wink:

:slight_smile:
Thanks for the info too, added to favorites

Ah, so I did. Sorry about that. I’ll fix my post above for future reference.

Don’t worry and thanks again for your help :ok_hand:

1 Like