Automation needs to join a text value and a variable when the luminescence changes

Second post, still very new to this…

I’m trying to set up an automation that fires whenever the luminescence of a Fibaro motion sensor changes (and then do the same with temperature).

When the automation fires, the intention is to send the entity value as part of the URL call to a rest api along with the other parameters the api is expecting.

In my automation.yaml, I currently have:

- id: '1595854635118'
  alias: 999-FM-FM1L
  description: Lux on FM1
   trigger:
     device_id: eee0ecfcaf3047479aaab3a3cd10cf3e
     platform: state
     entity_id: sensor.fibaro_system_fgms001_zw5_motion_sensor_luminance
   action:
  - data:`
    url_param: ?R=V&A=0&M=999-FM-FM1L&D=
{{sensor.fibaro_system_fgms001_zw5_motion_sensor_luminance}}
    service: rest_command.ccc_send

“urlparam” is the parameter I am sending to the rest api call.

?R=V&A=0&M=999-FM-FM1L&D= is the parameter string it is expecting and then

{{sensor.fibaro_system_fgms001_zw5_motion_sensor_luminance}}

On the occasions that the automation calls the rest api, it is sending as a string ({{sensor.fibaro_system_fgms001 etc), rather than a value.

I’m clearly wide of the mark on something, could someone put me out of my misery and explain what I am doing wrong (small words preferred)?

Thanks

Use data_template instead of data. Rule 1 here.

Also, please format your code correctly, as per rule 11 here. Your indentation might be all wrong, but we can’t tell with your current formatting.

Hi Troon,

Many thanks for your help.

I’ve changed data to data_template:

- id: '1595936114203'
  alias: 999-FM-FM1L
  description: ''
  trigger:
  - entity_id: sensor.fibaro_system_fgms001_zw5_motion_sensor_luminance
    platform: state
  condition: []
  action:
  - data_template:
      url_param: ?R=V&A=0&M=999-FM-FM1L&D={{sensor.fibaro_system_fgms001_zw5_motion_sensor_luminance}}
    service: rest_command.ccc_send

When it contained “data”, it was sending {{sensor.fibaro_system_fgms001_zw5_motion_sensor_luminance}}" as the value

After changing it to “data_template”, it is no longer sending the data to the rest api and I am getting the following error:

999-FM-FM1L: Error executing script. Unexpected error for call_service at pos 1: Error rendering data template: UndefinedError: ‘sensor’ is undefined

I’m sure I’ve made another obvious mistake, I’m just not sure what it is

You’re not getting the state. You just have an entity_id in there without quotes (to make it a string). See this post about templating types. This means that the template is trying to access a sensor object that doesn’t exist. You need to get the state from the state machine using one of the template functions, like states().

{{ states('sensor.fibaro_system_fgms001_zw5_motion_sensor_luminance') }}

Progress! :slight_smile:

- id: '1595936114203'
  alias: 999-FM-FM1L
  description: 'Lux level change on FM1'
  trigger:
  - entity_id: sensor.fibaro_system_fgms001_zw5_motion_sensor_luminance
    platform: state
  condition: []
  action:
  - data_template:
      url_param: ?R=V&A=0&M=999-FM-FM1L&D=
      url_value: '{{ states("sensor.fibaro_system_fgms001_zw5_motion_sensor_luminance") }}'
    service: rest_command.ccc_test

For some reason it required " instead of ’ in order to work (working line below):

 '{{ states("sensor.fibaro_system_fgms001_zw5_motion_sensor_luminance") }}'

Thanks to you both for your help - not sure which post I should mark as the solution? Both were very useful in getting me to the answer.

mark yours as it has the full solution

It shouldn’t matter if you use "{{ 'something' }}" or '{{ "something" }}' — but the inner and outer pairs must be different.

As Troon says : -

You will find the majority of examples on this forum (not all but go figure) to have " as outers and ’ as inners. I think this is mainly because it is often the case that there are multiple inner quotes and thus it makes the template shorter (and easier to read in my opinion too).
Regardless if you stick to one or the other it will make cutting and pasting less fraught with ‘wrong quotes’

Edit: And just to make matters worse. the GUI editor may step in and say "Nah ! I don’t like that, I’ll change everything to singles quoutes and double single quotes - Just to mess with your head ! "
:rofl:

1 Like