Color change delayed - Button card

Hi,
I have created the following code to change the background Color of a button card.
The code generally works but it always takes 10 seconds before the Color changes and sometime I need to reload the page. Any idea how to fix this?

  • background-color: |
    [[[
    return states[variables.room_entity].state === ‘on’ ? ‘rgb(129, 138, 129)’ : ‘rgb(245, 247, 248)’;
    ]]]